package cn.xingzhe.monster.user.infrastructure.persistence.entity;

import cn.xingzhe.monster.user.domain.enums.CaptchaTypeEnum;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.index.CompoundIndex;
import org.springframework.data.mongodb.core.index.Indexed;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.mapping.Field;

import java.time.LocalDateTime;

/**
 * 验证码持久化实体
 * 直接映射 MongoDB 数据库
 *
 * @author System
 * @version 1.0.0
 * @since 2024-01-01
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Document(collection = "captchas")
@CompoundIndex(name = "key_type_idx", def = "{'key': 1, 'type': 1}", unique = true)
public class CaptchaEntity {

    /**
     * 验证码ID
     */
    @Id
    private String id;

    /**
     * 键（手机号或邮箱）
     */
    @Field("key")
    private String key;

    /**
     * 验证码
     */
    @Field("code")
    private String code;

    /**
     * 验证码类型
     */
    @Field("type")
    private CaptchaTypeEnum type;

    /**
     * 过期时间
     */
    @Indexed(expireAfterSeconds = 0)
    @Field("expire_time")
    private LocalDateTime expireTime;

    /**
     * 创建时间
     */
    @Field("create_time")
    private LocalDateTime createTime;
}

